package com.dyna.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyna.core.utils.PageUtil;
import com.dyna.domain.resp.R;
import com.dyna.enums.CommonStatusEnum;
import com.dyna.foundation.domain.entity.SysMenu;
import com.dyna.foundation.domain.form.SysMenuSaveForm;
import com.dyna.foundation.domain.req.SysMenuListReq;
import com.dyna.foundation.domain.resp.SysMenuResp;
import com.dyna.foundation.domain.resp.SysMenuSimpleResp;
import com.dyna.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;

/**
 * @author zhoucaiwang
 * @date 2023-08-05
 */
@Api(value = "菜单")
@RestController
@RequestMapping(value = "/sys-menu")
public class SysMenuController {

    private final SysMenuService sysMenuService;
    @Autowired
    public SysMenuController(SysMenuService sysMenuService) {
        this.sysMenuService = sysMenuService;
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建菜单")
    public R<Long> createMenu(@Valid @RequestBody SysMenuSaveForm createReqVO) {
        Long menuId = sysMenuService.createMenu(createReqVO);
        return R.data(menuId);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改菜单")
    public R<Boolean> updateMenu(@Valid @RequestBody SysMenuSaveForm updateReqVO) {
        sysMenuService.updateMenu(updateReqVO);
        return R.data(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除菜单")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "菜单编号", required= true, example = "1024")})
    public R<Boolean> deleteMenu(@RequestParam("id") Long id) {
        sysMenuService.deleteMenu(id);
        return R.data(true);
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取菜单列表", notes = "用于【菜单管理】界面")
    public R<List<SysMenuResp>> getMenuList(SysMenuListReq reqVO) {
        List<SysMenu> list = sysMenuService.getMenuList(reqVO);
        list.sort(Comparator.comparing(SysMenu::getSort));
        return R.data(PageUtil.convertPageResult(list, SysMenuResp.class));
    }

    @GetMapping({"/list-all-simple", "simple-list"})
    @ApiOperation(value = "获取菜单精简信息列表",
            notes = "只包含被开启的菜单，用于【角色分配菜单】功能的选项。在多租户的场景下，会只返回租户所在套餐有的菜单")
    public R<List<SysMenuSimpleResp>> getSimpleMenuList() {
        SysMenuListReq req = new SysMenuListReq();
        req.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<SysMenu> list = sysMenuService.getMenuList(req);

        list = sysMenuService.filterDisableMenus(list);
        list.sort(Comparator.comparing(SysMenu::getSort));
        return R.data(PageUtil.convertPageResult(list, SysMenuSimpleResp.class));
    }

    @GetMapping("/get")
    @ApiOperation(value = "获取菜单信息")
    public R<SysMenuResp> getMenu(Long id) {
        SysMenu menu = sysMenuService.getMenu(id);
        return R.data(BeanUtil.toBean(menu, SysMenuResp.class));
    }


}